import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

Dio dio = new Dio();

class Shezhi extends StatefulWidget {
  Shezhi({Key key}) : super(key: key);

  _ShezhiState createState() => _ShezhiState();
}

class _ShezhiState extends State<Shezhi> {
  TextEditingController _password = TextEditingController();
  TextEditingController _newpassword = TextEditingController();
  var user_id;
  var number;
  var psw;
  getName() async {
    //获取Id
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    user_id = sharedPreferences.getString('user_id');

    number = sharedPreferences.getString('number');
    password();
    setState(() {});
  }

  void password() async {
    if (number.length == 10) {
      Response response = await dio
          .get('http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}');
      psw = response.data;
    } else {
      Response response = await dio
          .get('http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}');
      psw = response.data;
    }
    print(psw['password']);
    // print(cang);
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    getName();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('设置')),
      body: Container(
        padding: EdgeInsets.all(5.0),
        child: Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(5.0),
              alignment: Alignment.centerLeft,
              child: Text("修改密码",style: TextStyle(fontSize: 16.0)),
            ),
            TextFormField(
              controller: _password,
              decoration: InputDecoration(
                contentPadding: EdgeInsets.all(10.0),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(5.0),
                ),
                labelText: '请输入原密码',
              ),
            ),
            Container(
              height: 5.0,
            ),
            TextFormField(
              controller: _newpassword,
              decoration: InputDecoration(
                contentPadding: EdgeInsets.all(10.0),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(5.0),
                ),
                labelText: '请输入新的密码',
              ),
            ),
            InkWell(
              child: Container(
                alignment: Alignment.center,
                width: MediaQuery.of(context).size.width,
                height: 40.0,
                color: Colors.red,
                margin: EdgeInsets.all(5),
                child: Text(
                  "确认修改密码",
                  style: TextStyle(color: Colors.white, fontSize: 16.0),
                ),
              ),
              onTap: () async {
                if (_password.text == "${psw['password']}") {
                  if (number.length==10) {
                    var pp = await dio.patch(
                      'http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}',
                      data: {'password': "${_newpassword.text}"});
                  }else{
                    var pp = await dio.patch(
                      'http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}',
                      data: {'password': "${_newpassword.text}"});
                  }
                  _password.clear();
                  _newpassword.clear();
                  showDialog<Null>(
                    context: context,
                    barrierDismissible: false,
                    builder: (BuildContext context) {
                      return new AlertDialog(
                        content: new SingleChildScrollView(
                          child: new ListBody(
                            children: <Widget>[
                              new Text('密码修改成功'),
                            ],
                          ),
                        ),
                        actions: <Widget>[
                          new FlatButton(
                            child: new Text('确定'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ],
                      );
                    },
                  );
                } else {
                  showDialog<Null>(
                    context: context,
                    barrierDismissible: false,
                    builder: (BuildContext context) {
                      return new AlertDialog(
                        title: new Text('温馨提示'),
                        content: new SingleChildScrollView(
                          child: new ListBody(
                            children: <Widget>[
                              new Text('原密码输入有误'),
                            ],
                          ),
                        ),
                        actions: <Widget>[
                          new FlatButton(
                            child: new Text('确定'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ],
                      );
                    },
                  );
                }
              },
            )
          ],
        ),
      ),
    );
  }
}
